<?php
	require "config.php"; // NOTE:It's a best practice to keep your private information in different file which has access to limited people

	$con = mysql_connect("localhost",$db_user ,$db_pass);
	error_reporting(0);	
	// if an error was encountered, the script exits with an error message
	if (!$con){
		die('Could not connect: ' . mysql_error());
	
    }
	mysql_select_db($db_name, $con); //connecting with the database
	//$sql_fetch = "SELECT * FROM Person WHERE firstName = '$_POST[firstName]' and lastName = '$_POST[lastName]'"; 	
	//$result = mysql_query($sql_fetch); 

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Add New Student Details</title>
</head>
<style>
  html {
        overflow: scroll; 
      }
.scrollable{
   overflow: auto;
   width: 70px; /* adjust this width depending to amount of text to display */
   height: 80px; /* adjust height depending on number of options to display */
   border: 1px silver solid;
 }
body{background-image:url('Images/sjsu logo.jpg');
background-repeat: no-repeat;
  background position: top center;
  background-size: 100%;
  background-size: 1400px 1200px;
  position:relative;}
  
h3.pos_fixed{
color:#6D63A9;
position:relative;
top:25px;
left:30px;
}
#b{
position:relative;
top:140px;
left:320px;
}

a:link    {
  /* Applies to all unvisited links */
  text-decoration:  none;
  font-weight:      bold;
  color:          "#blue";
  font-size:120%;
  } 

</style>

<div id="b">
<div id = "e"> 

<? 
$sql_fetch = "SELECT * FROM Person WHERE firstName = '$_POST[firstName]' and lastName = '$_POST[lastName]'"; 	
	$result = mysql_query($sql_fetch);
$num_rows = mysql_num_rows($result);
   if ($num_rows == 0) {
	   echo "Person does not exist in the system! Please make an entry in the Person details before creating entry in Student details. <br> <br> <br>";
   }
	
	if ($num_rows > 0) {
	$sql_fetch = "SELECT SSN FROM Person WHERE firstName = '$_POST[firstName]' and lastName = '$_POST[lastName]'"; 	
	//$result = $conn->query($ssn);
	$result1 = mysql_query($sql_fetch);
	//$ssn = mysql_fetch_array($result1);
	//echo $ssn;
	$num_rows1 = mysql_num_rows($result1);

   if ($num_rows1 > 0) {
   while($row = mysql_fetch_array($result1)){
	//echo "SSN retrieved <br> <br> <br>";
	//   echo "<td>" . $row['SSN'] . "</td>";
	//echo "<td>" . $row['result1'] . "</td>"
	//echo "<td>" . $row['firstName'] . "</td>";
	//echo "<td>" . $row['lastName'] . "</td>";
	$sql_add = "INSERT INTO Student (SSN,firstName,lastName,GPA,major,term,fees) VALUES ('$row[SSN]','$_POST[firstName]','$_POST[lastName]','$_POST[GPA]','$_POST[major]','$_POST[term]','$_POST[fees]');";
	$result2 = mysql_query($sql_add);
	echo "Student entered in the system. <br> <br> <br>";
	}
	}
	}
	
   mysql_close($con);?>
</table>
<div id="c">
<div id="d"><br><br><a href="addStudent.php">Add New Student Page</a>
<div id="d"><br><br><a href="CreatePage.php">Home</a>
</body>
</html>
